# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=flif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgname=FLIF
pkgver=0.4
pkgrel=2
pkgdesc="Free Lossless Image Format (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/FLIF-hub/FLIF"
license=('GPL3')
conflicts=("${MINGW_PACKAGE_PREFIX}-flif-git")
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libpng" 
         "${MINGW_PACKAGE_PREFIX}-SDL2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/FLIF-hub/FLIF/archive/v${pkgver//_/-}.tar.gz"
        "0001-include-xmmintrin-on-x86-only.patch"
        "0002-use-avx-on-x86-only.patch")
sha256sums=('cc98313ef0dbfef65d72bc21f730edf2a97a414f14bd73ad424368ce032fdb7f'
            'e1e97dca22fce34cce8676589a686d37cea5759b71b9c1860292eccf6c5c9514'
            'f28767b7e8c843dbf6b7654baf4093b7e9ff3af7d1908f25d2ba957bcaac37e9')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-include-xmmintrin-on-x86-only.patch \
    0002-use-avx-on-x86-only.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      "${srcdir}/${_pkgname}-${pkgver//_/-}/src"

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/ctest
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/cmake --install .
}
